//引入gulp
const gulp = require('gulp')
//引入cssmin
const cssmin = require('gulp-cssmin')
//引入sass和gulpsass
const sass = require('gulp-sass')(require('sass'))

//引入gulp-uglify js压缩处理
const uglify = require('gulp-uglify')
//引入gulp-babel es6转es5
const bable = require('gulp-babel')

//引入gulp-htmlmin 压缩html文件
const htmlmin = require('gulp-htmlmin')
//引入gulp-clean
const clean = require('gulp-clean')
//引入webserver
const webserver = require('gulp-webserver')

/* 打包css任务 */
gulp.task('css', function () {
    return gulp.src('./src/css/**')
        .pipe(cssmin()) //压缩css
        .pipe(gulp.dest('./dist/css'))
})

/* 打包sass文件编译成css,并压缩处理  */
gulp.task('sass',function(){
    return gulp.src('./src/sass/**')
            .pipe(sass()) //scss->css
            .pipe(cssmin()) //压缩处理
            .pipe(gulp.dest('./dist/css'))
})

/* 打包js文件， 压缩js, es6->es5 */
gulp.task('js',function(){
    return gulp.src('./src/js/**')
        // .pipe(bable({
        //     presets:['es2015']
        // }))
        // .pipe(uglify())
        .pipe(gulp.dest('./dist/js'))
})

/* 打包html文件 压缩处理 */
gulp.task('html',function(){
    return gulp.src('./src/pages/**')
        .pipe(htmlmin({
            removeEmptyAttributes:true,
            collapseWhitespace:true
        }))
        .pipe(gulp.dest('./dist/pages'))
})

/* 打包 static 和 lib第三方文件 */
gulp.task('static',function(){
    return gulp.src('./src/static/**')
            .pipe(gulp.dest('./dist/static'))
})

gulp.task('lib',function(){
    return gulp.src('./src/lib/**')
            .pipe(gulp.dest('./dist/lib'))
})

/* 清除dist目录 */
gulp.task('clean',function(){
    return gulp.src('./dist',{allowEmpty:true})
            .pipe(clean())
})

/* webserver任务 */
gulp.task('webserver',function(){
    return gulp.src('./dist')
            .pipe(webserver({
                host:'localhost',
                port:3000,
                livereload:true,
                open:'./pages/index.html'
            }))
})

/* 侦听任务 */
gulp.task('watch',function(){
    gulp.watch('./src/css/**', gulp.series('css'))
    gulp.watch('./src/js/**', gulp.series('js'))
    gulp.watch('./src/sass/**', gulp.series('sass'))
    gulp.watch('./src/pages/**', gulp.series('html'))
    gulp.watch('./src/static/**', gulp.series('static'))
    gulp.watch('./src/lib/**', gulp.series('lib'))
})

/* 按顺序批量执行任务 */
gulp.task('task1', gulp.series('css','sass','js','html','static','lib') ) 

/* 并发同时批量执行任务 */
gulp.task('task2', gulp.parallel('css','sass','js','html','static','lib') ) 

/* 定义默认任务 直接通过gulp执行*/
// exports.default = gulp.series('clean',gulp.parallel('css','sass','js','html','static','lib'))
exports.default = gulp.series('clean',gulp.parallel('css','sass','js','html','static','lib'), 'webserver','watch')
